package se.curalia.slimcache;

import java.util.Properties;
import java.util.Hashtable;
import java.io.IOException;
import java.io.InputStream;
import org.apache.log4j.*;

/**
 * Rudimentary config
 */
public class Configuration {


    public static final Logger log = Logger.getLogger(Configuration.class.getName());


    private static final String BASEDIR = "basedir";
    private static final String LAYERS = "layers";
    private static final String SERVER_URI = "uri";
    private static final String ZOOMLEVELS = "zoomlevels";
    private static final String SRS = "srs";
    private static final String BBOX = "bbox";
    private static final String SCALES = "scales";
    private static final String CONNECT_TIMEOUT = "connectTimeout";
    private static final String READ_TIMEOUT = "readTimeout";

    private static final String DEFAULT_CONNECT_TIMEOUT = "30000";
    private static final String DEFAULT_READ_TIMEOUT = "90000";

    Properties props;
    Hashtable<String, Layer> layers;
    Hashtable<Integer, Double> resolutions;
    Hashtable<String,String> mimeTypes;
    private int readTimeout;
    private int connectTimeout;



    public Configuration() throws Exception {
        loadProperties();
    }

    private void loadProperties() throws IOException {
        props = new Properties();
        InputStream in = this.getClass().getResourceAsStream("/slimcache.properties");
        if (in == null) {
            log.error("Could not find configuration file: /slimcache.poperties");
            throw new IOException("Could not find configuration file slimcache.poperties");
        }
        props.load(in);

        layers = new Hashtable<String, Layer>();
        String confLayers = (String) props.get(LAYERS);
        if (layers == null) {
            throw new IllegalArgumentException("No layers defined");
        }
        String[] layernames = confLayers.split(",");
        for (int i = 0; i < layernames.length; i++) {
            String layername = layernames[i];
            Layer l = new Layer();

            // Set layername
            l.setName(layername);

            // Set server URI
            l.setUri((String) props.get(layername + "." + SERVER_URI));

            // Set zoomLevels
            String zoomLevels = (String) props.get(layername + "." + ZOOMLEVELS);
            if (zoomLevels != null) {
                l.setZoomLevels(Integer.parseInt(zoomLevels));
            }

            //
            l.setLayers((String) props.get(layername + "." + LAYERS));


            // Set BBOX
            String bbox = (String) props.get(layername + "." + BBOX);
            l.setBbox(new BBOX(bbox));

            String srs = ((String) props.get(layername + "." + SRS));
            l.setSrs(srs);
            layers.put(layername, l);
            log.info("configured layer: " + layername);

        }

        mimeTypes = new Hashtable<String,String>();
        mimeTypes.put("image/jpeg", "jpeg");
        mimeTypes.put("image/gif", "gif");
        mimeTypes.put("image/png", "png");

       	readTimeout = Integer.parseInt(props.getProperty(
       			READ_TIMEOUT, DEFAULT_READ_TIMEOUT));

       	connectTimeout = Integer.parseInt(props.getProperty(
       			CONNECT_TIMEOUT, DEFAULT_CONNECT_TIMEOUT));

    }

    public String getSuffix(String mimeType) {
        return mimeTypes.get(mimeType);
    }

    public String getBaseDir() {
        
        return (String) props.get(BASEDIR);
    }

    public Layer getLayer(String layername) {
        return layers.get(layername);
    }

    public int getConnectTimeout() {
    	return connectTimeout;
    }

    public int getReadTimeout() {
    	return readTimeout;
    }
}
